package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.mapper.NodeMapper;
import com.heima.pojo.dto.NodeAddDto;
import com.heima.pojo.dto.NodeSearchDto;
import com.heima.pojo.dto.NodeUpdateDto;
import com.heima.pojo.dto.RegionDetailSearchDto;
import com.heima.pojo.entity.Node;
import com.heima.pojo.vo.NodeDetailVo;
import com.heima.pojo.vo.NodeVo;
import com.heima.pojo.vo.PageResponseVo;
import com.heima.service.NodeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NodeServiceImpl extends ServiceImpl<NodeMapper, Node> implements NodeService {

    @Autowired
    private NodeMapper nodeMapper;

    @Override
    public PageResponseVo<NodeVo> searchNode(NodeSearchDto nodeSearchDto) {
        // 创建分页对象
        //   IPage<NodeVo> page = new Page<>(nodeSearchDto.getPageIndex(), nodeSearchDto.getPageSize());
        Page<NodeVo> page = new Page<>(nodeSearchDto.getPageIndex(), nodeSearchDto.getPageSize());
        // 执行分页查询
        IPage<NodeVo> nodeVoIPage = nodeMapper.searchNode(page, nodeSearchDto);
        // 构造分页响应对象
        PageResponseVo<NodeVo> responseVo = new PageResponseVo<>();
        responseVo.setPageSize(nodeSearchDto.getPageSize());
        responseVo.setPageIndex(nodeSearchDto.getPageIndex());
        responseVo.setTotalCount(nodeVoIPage.getTotal());
        responseVo.setTotalPage(nodeVoIPage.getPages());
        responseVo.setCurrentPageRecords(nodeVoIPage.getRecords());
        return responseVo;
    }

    @Override
    public int addNode(NodeAddDto nodeAddDto) {
        Node node = new Node();
        BeanUtils.copyProperties(nodeAddDto, node);
        node.setCreateUserId(1);
        return nodeMapper.insert(node);
    }

    @Override
    public List<NodeDetailVo> searchNodeDetailById(Long nodeId) {
        return nodeMapper.selectNodeDetailById(nodeId);

    }

    @Override
    public Boolean updateNodeBynodeId(Long nodeId, NodeUpdateDto nodeUpdateDto) {
        LambdaQueryWrapper<Node> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Node::getId, nodeId);
        Node node = new Node();
        BeanUtils.copyProperties(nodeUpdateDto, node);
        node.setCreateUserId(1);
        return this.update(node, wrapper);
    }

    @Override
    public Boolean deleteNodeBynodeId(Long nodeId) {
        return this.removeById(nodeId);
    }
}
